package  
{
	import flash.display.MovieClip;
	import flash.events.Event;
	
	/**
	 * ...
	 * @author Rafael Bringel
	 */
	public class Main extends MovieClip
	{	
		var mapa : Array = new Array();
		var tela = new MovieClip();
		var bloco : MovieClip;
		var personagem : Personagem;
		var totem : Totem;
		private static var _instance:Main;
   
	
		public function Main()
		{
			_instance = this;
			mapa = 	[
						[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
						[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
						[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
						[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
						[0, 0, 1, 0, 0,10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
						[0, 0, 1, 0, 0, 1, 1, 1, 2, 2, 1, 3, 3, 1, 1, 0, 0, 1, 0, 0],
						[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0],
						[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0],
						[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0],
						[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0],
						[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 4, 1, 1, 0, 1, 0, 0],
						[0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 6, 0, 7, 0, 8, 0, 9, 1, 0, 0],
						[0, 0, 1, 5, 5, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0],
						[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
						[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
					];
					

			desenharMapa();
			addEventListener(Event.ENTER_FRAME, update, false, 0, true);
			stage.addChild(new movieMonitor());
		}
		
		private function update(Evento:Event):void
		{
			personagem.update();
			verifyCollision();
			Input.onUpdate();
			Aura.update();
		}
		
		private function desenharMapa():void
		{
			tela = new MovieClip();
			addChild(tela);
			
			for (var v:int = 0; v < mapa.length; v++) 
			{
				for (var h:int = 0; h < mapa[0].length; h++) 
				{
					switch(mapa[v][h])
					{
						case 1:
						case 2:
						case 3:
						case 4:
						case 5:
							if(mapa[v][h] == 1)
								bloco = new Bloco(ElementalState.NORMAL);
							else if(mapa[v][h] == 2)
								bloco = new Bloco(ElementalState.FOGO);
							else if(mapa[v][h] == 3)
								bloco = new Bloco(ElementalState.AGUA);
							else if(mapa[v][h] == 4)
								bloco = new Bloco(ElementalState.TERRA);
							else if(mapa[v][h] == 5)
								bloco = new Bloco(ElementalState.AR);
								
							bloco.x = (h * 32) + 16;
							bloco.y = (v * 32) + 16;
							
							if ((v % 2) == 0)
							{
								if ((h % 2) == 0)
								{
									//normal
								}
								else
								{
									bloco.rotation = 180;
								}
							}
							else
							{
								if ((h % 2) == 0)
								{
									bloco.rotation = 180;
								}
								else
								{
									//normal
								}
							}
							
							mapa[v][h] = bloco;
							tela.addChild(bloco);
							
							break;		
						
						case 6:
						case 7:
						case 8:
						case 9:
							if(mapa[v][h] == 6)
								totem = new Totem(ElementalState.FOGO);
							else if(mapa[v][h] == 7)
								totem = new Totem(ElementalState.AGUA);
							else if(mapa[v][h] == 8)
								totem = new Totem(ElementalState.TERRA);
							else if(mapa[v][h] == 9)
								totem = new Totem(ElementalState.AR);
								
							totem.x = (h * 32) + 16;
							totem.y = (v * 32) + 20;
							mapa[v][h] = totem;
							tela.addChild(totem);
							break;
							
						case 10: 
							personagem = new Personagem(ElementalState.AGUA);
							personagem.x = (h * 32) + 16;
							personagem.y = (v * 32) + 16;
							tela.addChild(personagem);
							break;
					}

				}
			}

			tela.setChildIndex(personagem, tela.getChildIndex(mapa[12][17]));
		}
		
		private function verifyCollision()
		{
			for (var v:int = 0; v < mapa.length; v++) 
			{
				for (var h:int = 0; h < mapa[0].length; h++) 
				{
					if (mapa[v][h] is Bloco)
					{
						if (personagem.hittestCorpo.hitTestObject(mapa[v][h]))
						{
							personagem.handleCollision(mapa[v][h]);
						}
					}
				}
			}
			
			if (personagem.hittestCorpo.hitTestObject(gravidade))
			{
				personagem.velocityY -= 0.6;
			}
		}
		
		public static function get instance():Main 
		{ 
			return _instance; 
		};
		
		public static function randomNumber(low:Number, high:Number):Number
		{
		  return Math.floor(Math.random() * (1+high-low)) + low;
		}
		
	}

}